
#include "pins.h"
#include "BitBand.h"

#define VERSION         "20240729"
#define IDENT           "EEPROM_STM"

// Buffers
#define BUFF_LEN 512

uint8_t pageBuffer[BUFF_LEN];

uint32_t address = 0;

uint32_t buff = 128;
//写入方式，1字节 128字节 256字节
uint16_t page = 1;

uint8_t byte1 = 0;
uint8_t buff1[1];
uint8_t buff4[4];

uint8_t b8;
uint16_t b16;
uint32_t b32;


void setup() {
  read_mode();

  //地址线
  addressOutput();

  pinMode(PB2, INPUT);
  
  pinMode(WE, OUTPUT);
  pinMode(CE, OUTPUT);
  pinMode(OE, OUTPUT);
  digitalWrite(WE, HIGH);
  digitalWrite(CE, HIGH);
  digitalWrite(OE, HIGH);

  // 初始化地址
  setAddress(0);

  Serial.begin(1000000);
//  Serial.begin(115200);
}





//---------------------------------------------------------






// 读取命令
uint8_t cmd;

void loop() {
    //程序判断用
    if (Serial.available()) {
        cmd = Serial.read();
        
        switch (cmd) {
            case 0x02:
                Serial.println("EEPROM_STM");break;
            case 0x03:
                //Serial.println("20240729");break;
                Serial.println(VERSION);break;
            case 0x04:
                Serial.println("ok");break;

            // 读取芯片ID
            case 0x05:
                // 清理缓存
                pageBuffer[0] = 0xff;
                pageBuffer[1] = 0xff;
                P_WE = 1;
                P_OE = 1;
                P_CE = 1;

                // 进入ID读取模式
                write_mode();
                setByte (0x5555, 0xaa);
                setByte (0x2aaa, 0x55);
                setByte (0x5555, 0x90);

                // 读取ID
                read_mode();
                P_OE = 0;
                P_CE = 0;
                pageBuffer[0] = getByte(0);
                pageBuffer[1] = getByte(1);
                P_OE = 1;
                P_CE = 1;

                // 退出ID读取模式
                write_mode();
                setByte (0x5555, 0xaa);
                setByte (0x2aaa, 0x55);
                setByte (0x5555, 0xF0);
                
                Serial.write(pageBuffer[0]);
                Serial.write(pageBuffer[1]);
                
                read_mode();
                break;

                
            // 复位    
            case 0x0F:
                digitalWrite(RESET, LOW);
                delayMicroseconds(10);
                digitalWrite(RESET, HIGH);
                break;

            // 读
            case 0xA0:
                read29();
                setAddress(0);
                break;
            // 写
            case 0xB0:
                
                buff4[0] = Serial.read();
                buff4[1] = Serial.read();
                buff4[2] = Serial.read();
                buff4[3] = Serial.read();
                
                address = (buff4[0]<<24)+(buff4[1]<<16)+(buff4[2]<<8)+buff4[3];
                
                write29();
                break;
                
            // 写测试
            case 0xB1:
                
                buff4[0] = Serial.read();
                buff4[1] = Serial.read();
                buff4[2] = Serial.read();
                buff4[3] = Serial.read();
                
                address = (buff4[0]<<24)+(buff4[1]<<16)+(buff4[2]<<8)+buff4[3];
                
                for (uint8_t i = 0; i < buff; i++) {
                    while (!Serial.available());
                    pageBuffer[i] = Serial.read();
                }

                
                for (uint8_t i = 0; i < buff; i++) {
                  Serial.write(pageBuffer[i] & 0xff);
                }
                
                break;

            // 擦除
            case 0xC0:
                erase();
                break;

            // 锁
//            case 0xD0:
//                lock();break;
            // 解锁
//            case 0xD1:
//                unlock();break;

            case 0xE1:
                //Serial.println(address  & 0xffffffff);
                Serial.write((address>>24) & 0xff);
                Serial.write((address>>16) & 0xff);
                Serial.write((address>>8) & 0xff);
                Serial.write(address & 0xff);
                Serial.println("");
                break;

            case 0xE2:
                //Serial.println(buff & 0xffff);
                Serial.write((buff>>8) & 0xff);
                Serial.write(buff & 0xff);
                Serial.println("");
                break;

            case 0xE3:
                Serial.write((page>>8) & 0xff);
                Serial.write(buff & 0xff);
                Serial.println("");
                break;
                break;



            // 参数配置
                
            // 地址
            case 0xF1:
                buff4[0] = Serial.read();
                buff4[1] = Serial.read();
                buff4[2] = Serial.read();
                buff4[3] = Serial.read();
                address = (buff4[0]<<24)+(buff4[1]<<16)+(buff4[2]<<8)+buff4[3];
                break;
            // 缓存
            case 0xF2:
                buff4[0] = Serial.read();
                buff4[1] = Serial.read();
                buff = (buff4[0]<<8)+buff4[1];
                break;
            // 1字节 128字节 256字节
            case 0xF3:
                buff4[0] = Serial.read();
                buff4[1] = Serial.read();
                page = (buff4[0]<<8)+buff4[1];
                break;
                
            default:    break;

        }
    }
    
}
